/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yunhoo.cqq.impl;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.yunhoo.cqq.model.Announcement;
import com.yunhoo.cqq.model.CodeBase;
import com.yunhoo.cqq.model.PublicityData;
import com.yunhoo.cqq.util.StrUtil;
import com.yunhoo.sys.db.util.IbatisTemplate;
import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Administrator
 */
public class IbatisAnnounce {

    private final static String NAMESPACE  = "announce.";
    private static SqlMapClient sqlmapclient = IbatisTemplate.getSqlMapClient();

    public static List getRootPublicityType() {
        List<PublicityData> list = new ArrayList();
        try {
            sqlmapclient.startTransaction();
            list = sqlmapclient.queryForList(NAMESPACE+"getRootPublicityType",new PublicityData());
            sqlmapclient.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println(NAMESPACE+"查询错误getRootDocType~~");
        } finally {
            try {
                sqlmapclient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    public static List getSubPublicityType(PublicityData publicityData) {
        List<PublicityData> list = new ArrayList();
        try {
            sqlmapclient.startTransaction();
            list = sqlmapclient.queryForList(NAMESPACE+"getSubPublicityType", publicityData);
            sqlmapclient.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println(NAMESPACE+"查询错误getSubDocType~~");
        } finally {
            try {
                sqlmapclient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    public static void insertAnnounce(Announcement announce) {//插入cf_announcement上传的文档
        try {
            sqlmapclient.startTransaction();
            sqlmapclient.insert(NAMESPACE+"insertAnnounce", announce);
            sqlmapclient.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println(NAMESPACE+"查询错误insertAnnouncement~~");
        } finally {
            try {
                sqlmapclient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static List getAllAnnounce() {  //获得cf_announcement所有公告
        List<Announcement> list = new ArrayList();
        try {
            sqlmapclient.startTransaction();
            list = sqlmapclient.queryForList(NAMESPACE+"getAllAnnounce",new Announcement());
            sqlmapclient.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println(NAMESPACE+"查询错误getAllAnnounce~~");
        } finally {
            try {
                sqlmapclient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    public static Object getAnnounceById(String id) {  //根据id获得公告
        Announcement announce = new Announcement();
        try {
            sqlmapclient.startTransaction();
            Announcement param = new Announcement();
            param.setId(id);
            announce = (Announcement) sqlmapclient.queryForObject(NAMESPACE+"getAnnounceById", param);
            if (announce != null) {
//                String filepath1 = FacesUtil.getServletContext().getRealPath(NAMESPACE+"/");
//                String filepath = filepath1 + announce.getUrl();
                File file = new File(announce.getUrl());
                if (file.exists()) {
                    announce.setShowflag(true);
                } else {
                    announce.setShowflag(false);
                }
            }
            if (announce.getUrl() != null) {
                String name = StrUtil.getShortPathWin(announce.getUrl());
                announce.setDocname(name);
            }
            sqlmapclient.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println(NAMESPACE+"查询错误getAnnounceById~~");
        } finally {
            try {
                sqlmapclient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return announce;
    }

    public static List getTop3Announce() {  //获得最新3条公告
        List<Announcement> list = new ArrayList();
        try {
            sqlmapclient.startTransaction();
            list = sqlmapclient.queryForList(NAMESPACE+"getTop3Announce" ,new Announcement());
            sqlmapclient.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println(NAMESPACE+"查询错误getTop3Announce~~");
        } finally {
            try {
                sqlmapclient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    public static List getTop5Announce() {  //获得最新3条公告
        List<Announcement> list = new ArrayList();
        try {
            sqlmapclient.startTransaction();
            list = sqlmapclient.queryForList(NAMESPACE+"getTop5Announce",new Announcement());
            if (list != null && list.size() > 0) {
                for (Announcement doc : list) {               
                    File file = new File(doc.getUrl());
                    if (file.exists()) {
                        doc.setShowflag(true);
                    } else {
                        doc.setShowflag(false);
                    }
                    if (doc.getUrl() != null) {
                        String name = StrUtil.getShortPathWin(doc.getUrl());
                        doc.setDocname(name);
//                        String str = new String(doc.getUrl().getBytes(NAMESPACE+"utf-8"), "iso-8859-1");
//                        doc.setUrl(str);
                    }

                }
            }

            sqlmapclient.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println(NAMESPACE+"查询错误getTop5Announce~~");
        } finally {
            try {
                sqlmapclient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    public static List getLast10Dayslist() {  //获得最近10天 公告
        List<Announcement> list = new ArrayList();
        try {
            sqlmapclient.startTransaction();
            list = sqlmapclient.queryForList(NAMESPACE+"getLast10Dayslist");
            if (list != null && list.size() > 0) {
                for (Announcement doc : list) {            
                    File file = new File(doc.getUrl());
                    if (file.exists()) {
                        doc.setShowflag(true);
                    } else {
                        doc.setShowflag(false);
                    }

                }
            }
            sqlmapclient.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println(NAMESPACE+"getLast10Dayslist~~");
        } finally {
            try {
                sqlmapclient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return list;
    }


    public static List getLeavingAnnounce() {  //获得最新3条公告
        List<Announcement> list = new ArrayList();
        try {
            sqlmapclient.startTransaction();
            list = sqlmapclient.queryForList(NAMESPACE+"getLeavingAnnounce",new Announcement());
            if (list != null && list.size() > 0) {
                for (Announcement doc : list) {            
                    File file = new File(doc.getUrl());
                    if (file.exists()) {
                        doc.setShowflag(true);
                    } else {
                        doc.setShowflag(false);
                    }

                }
            }
            sqlmapclient.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println(NAMESPACE+"查询错误getLeavingAnnounce~~");
        } finally {
            try {
                sqlmapclient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    public static Object getTop1Announce() {  //获得最新公告
        Announcement announce = new Announcement();
        try {
            sqlmapclient.startTransaction();
            announce = (Announcement) sqlmapclient.queryForObject(NAMESPACE+"getTop1Announce", new Announcement());
            sqlmapclient.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println(NAMESPACE+"查询错误getTop1Announce~~");
        } finally {
            try {
                sqlmapclient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return announce;
    }

    public static List getSelectAnnounce(String id) {  //获得cf_announcement某类型的公告
        List<Announcement> list = new ArrayList();
        try {
            sqlmapclient.startTransaction();
            Announcement param = new Announcement();
            param.setId(id);
            list = sqlmapclient.queryForList(NAMESPACE+"getSelectAnnounce", param);
            if (list != null && list.size() > 0) {
                for (Announcement doc : list) {              
                    File file = new File(doc.getUrl());
                    if (file.exists()) {
                        doc.setShowflag(true);
                    } else {
                        doc.setShowflag(false);
                    }

                }
            }

            sqlmapclient.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println(NAMESPACE+"查询错误getSelectAnnounce~~");
        } finally {
            try {
                sqlmapclient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    public static void deleteSelectAnnounce(Announcement announce) {//删除选中公告
        try {
            sqlmapclient.startTransaction();
            sqlmapclient.delete(NAMESPACE+"deleteSelectAnnounce", announce);
            sqlmapclient.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println(NAMESPACE+"查询错误deleteSelectAnnounce~~");
        } finally {
            try {
                sqlmapclient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void deletepublicityByid(PublicityData announce) {//删除选中公告
        try {
            sqlmapclient.startTransaction();
            sqlmapclient.delete(NAMESPACE+"deletepublicityByid", announce);
            sqlmapclient.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println(NAMESPACE+"查询错误deleteSelectAnnounce~~");
        } finally {
            try {
                sqlmapclient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void updateSelectAudit(Announcement announce) {//更新公告审核状态
        try {
            sqlmapclient.startTransaction();
            sqlmapclient.update(NAMESPACE+"updateSelectAudit", announce);
            sqlmapclient.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println(NAMESPACE+"查询错误updateSelectAudit~~");
        } finally {
            try {
                sqlmapclient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void cancelSelectAudit(Announcement announce) {//取消公告审核
        try {
            sqlmapclient.startTransaction();
            sqlmapclient.update(NAMESPACE+"cancelSelectAudit", announce);
            sqlmapclient.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println(NAMESPACE+"查询错误cancelSelectAudit~~");
        } finally {
            try {
                sqlmapclient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void updateAnnounce(Announcement announce) {//更新公告
        try {
            sqlmapclient.startTransaction();
            sqlmapclient.update(NAMESPACE+"updateAnnounce", announce);
            sqlmapclient.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println(NAMESPACE+"查询错误updateAnnounce~~");
        } finally {
            try {
                sqlmapclient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static List checkPublicityRepeat(PublicityData type) { //获得所选文档类型节点的所有文档
        List<PublicityData> list = new ArrayList();
        try {
            sqlmapclient.startTransaction();
            list = sqlmapclient.queryForList(NAMESPACE+"checkPublicityRepeat", type);
            sqlmapclient.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println(NAMESPACE+"查询错误checkDocRepeat~~");
        } finally {
            try {
                sqlmapclient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    public static List checkPublicityParent(PublicityData type) { //查看是否有父节点
        List<PublicityData> list = new ArrayList();
        try {
            sqlmapclient.startTransaction();
            list = sqlmapclient.queryForList(NAMESPACE+"checkPublicityParent", type);
            sqlmapclient.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println(NAMESPACE+"查询错误checkDoctypeParent~~");
        } finally {
            try {
                sqlmapclient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    public static void insertPublicitytype(PublicityData base) {//插入doctype
        try {
            sqlmapclient.startTransaction();
            sqlmapclient.update(NAMESPACE+"insertPublicitytype", base);
            sqlmapclient.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println(NAMESPACE+"查询错误insertDoctype~~");
        } finally {
            try {
                sqlmapclient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void updatePublicitytype(PublicityData base) {//更新doctype
        try {
            sqlmapclient.startTransaction();
            sqlmapclient.update(NAMESPACE+"updatePublicitytype", base);
            sqlmapclient.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println(NAMESPACE+"查询错误updateDoctype~~");
        } finally {
            try {
                sqlmapclient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void updatePublicitytypeIsEnd(CodeBase base) {//更新doctype isEnd属性
        try {
            sqlmapclient.startTransaction();
            sqlmapclient.update(NAMESPACE+"updatePublicitytypeIsEnd", base);
            sqlmapclient.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println(NAMESPACE+"查询错误updateDoctypeIsEnd~~");
        } finally {
            try {
                sqlmapclient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static List getPublicitySameLevel(CodeBase base) { //查看是否有同级节点
        List<PublicityData> list = new ArrayList();
        try {
            sqlmapclient.startTransaction();
            list = sqlmapclient.queryForList(NAMESPACE+"getPublicitySameLevel", base);
            sqlmapclient.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println(NAMESPACE+"查询错误getDoctypeSameLevel~~");
        } finally {
            try {
                sqlmapclient.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return list;
    }
}
